#include "TileCacheOther.h"
#include "PathFinder.h"
#include "../Contrib/fastlz/fastlz.h"
//#include <Finder/TileCacheOther.h>
//#include <Finder/PathFinder.h>
//#include <Contrib/fastlz/fastlz.h>

 MeshProcess::MeshProcess() : m_geom(0)
{
}

inline void MeshProcess::init(InputGeom* geom)
{
	m_geom = geom;
}

void MeshProcess::process(struct dtNavMeshCreateParams* params,
	unsigned char* polyAreas, unsigned short* polyFlags)
{
	// Update poly flags from areas.
	for (int i = 0; i < params->polyCount; ++i)
	{
		if (polyAreas[i] == DT_TILECACHE_WALKABLE_AREA)
			polyAreas[i] = SAMPLE_POLYAREA_GROUND;

		if (polyAreas[i] == SAMPLE_POLYAREA_GROUND ||
			polyAreas[i] == SAMPLE_POLYAREA_GRASS ||
			polyAreas[i] == SAMPLE_POLYAREA_ROAD)
		{
			polyFlags[i] = SAMPLE_POLYFLAGS_WALK;
		}
		else if (polyAreas[i] == SAMPLE_POLYAREA_WATER)
		{
			polyFlags[i] = SAMPLE_POLYFLAGS_SWIM;
		}
		else if (polyAreas[i] == SAMPLE_POLYAREA_DOOR)
		{
			polyFlags[i] = SAMPLE_POLYFLAGS_WALK | SAMPLE_POLYFLAGS_DOOR;
		}
	}

	// Pass in off-mesh connections.
	if (m_geom)
	{
		params->offMeshConVerts = m_geom->getOffMeshConnectionVerts();
		params->offMeshConRad = m_geom->getOffMeshConnectionRads();
		params->offMeshConDir = m_geom->getOffMeshConnectionDirs();
		params->offMeshConAreas = m_geom->getOffMeshConnectionAreas();
		params->offMeshConFlags = m_geom->getOffMeshConnectionFlags();
		params->offMeshConUserID = m_geom->getOffMeshConnectionId();
		params->offMeshConCount = m_geom->getOffMeshConnectionCount();
	}
}

int FastLZCompressor::maxCompressedSize(const int bufferSize)
{
	return (int)(bufferSize * 1.05f);
}

dtStatus FastLZCompressor::compress(const unsigned char* buffer, const int bufferSize,
	unsigned char* compressed, const int /*maxCompressedSize*/, int* compressedSize)
{
	*compressedSize = fastlz_compress((const void* const)buffer, bufferSize, compressed);
	return DT_SUCCESS;
}

dtStatus FastLZCompressor::decompress(const unsigned char* compressed, const int compressedSize,
	unsigned char* buffer, const int maxBufferSize, int* bufferSize)
{
	*bufferSize = fastlz_decompress(compressed, compressedSize, buffer, maxBufferSize);
	return *bufferSize < 0 ? DT_FAILURE : DT_SUCCESS;
}
